package com.kyg.tjtoolmanager.controller;

import com.alibaba.fastjson.JSONArray;
import com.kyg.tjtoolmanager.App;
import com.kyg.tjtoolmanager.db.SQLiteUtils;
import com.kyg.tjtoolmanager.module.WareHouse;
import com.kyg.tjtoolmanager.net.http.HttpBack;
import com.kyg.tjtoolmanager.net.http.HttpEntity;
import com.kyg.tjtoolmanager.net.http.HttpService;
import com.kyg.tjtoolmanager.net.http.HttpURL;
import com.kyg.tjtoolmanager.ui.ProgressStage;
import com.kyg.tjtoolmanager.ui.StageUIHelper;
import io.netty.util.internal.StringUtil;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import okhttp3.Call;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;


public class MainController extends BaseController {
    @FXML
    private TextField FieldName;
    @FXML
    private TextField FieldPwd;

    @Override
    public void onShow() {
        super.onShow();
        SQLiteUtils.insertLog("MainController onShow");
        if (StringUtil.isNullOrEmpty(App.globalVal.httpHost)) {
            StageUIHelper.getInstance().showTipAlert("服务器信息为空，请设置完成后重新打开本应用");
            return;
        }
        //获取仓库信息
        ProgressStage progressStage = ProgressStage.of((Stage) rootLayout.getScene().getWindow(), "正在获取库房信息");
        progressStage.show();

        Map<String, String> postParams = new HashMap<>();
        postParams.put("warehouse_id", "0");
        HttpService.getInstance().httpGet(HttpURL.URL(HttpURL.GET_HOUSE_INFO), postParams, new HttpBack() {
            @Override
            public void onSuccess(Call call, HttpEntity httpEntity) {
                progressStage.close();
                SQLiteUtils.insertLog("库房接口返回：" + httpEntity.toString());
                try {
                    if (httpEntity.getCode() == 1) {
                        App.wareHouses = JSONArray.parseArray(httpEntity.getData().toString(), WareHouse.class);
                        if (App.wareHouses.size() == 0) {
                            StageUIHelper.getInstance().showTipAlert("库房信息为空，请在后台添加库房信息。\n重启本应用即可重新获取库房信息");
                        }
                    } else {
                        StageUIHelper.getInstance().showTipAlert(httpEntity.getMsg());
                    }
                } catch (Exception ex) {
                    SQLiteUtils.insertLog("库房接口解析错误：" + ex.getMessage());
                }
            }

            @Override
            public void onFail(Call call, String e) {
                progressStage.close();
                SQLiteUtils.insertLog("库房接口错误：" + e);
                StageUIHelper.getInstance().showTipAlert(e);
            }
        });
    }

    @FXML
    protected void onLoginClick() {
        if (StringUtil.isNullOrEmpty(App.globalVal.httpHost)) {
            showToSetting("请先设置服务器等信息再使用！");
            return;
        }
        String name = FieldName.getText().toString().trim();
        String pwd = FieldPwd.getText().toString().trim();
        if (StringUtil.isNullOrEmpty(name) || StringUtil.isNullOrEmpty(pwd)) {
            StageUIHelper.getInstance().showTipAlert("用户名或密码不能为空！");
            return;
        }
        if (App.DEBUG) {
            StageUIHelper.toCustomUI("库房管理", "house-view.fxml");
            return;
        }
        ProgressStage progressStage = ProgressStage.of((Stage) rootLayout.getScene().getWindow(), "正在登陆");
        progressStage.show();

        Map<String, String> postParams = new HashMap<>();
        postParams.put("mobile", name);
        postParams.put("password", pwd);
        HttpService.getInstance().httpPostMap(HttpURL.URL(HttpURL.LOGIN), postParams, new HttpBack() {
            @Override
            public void onSuccess(Call call, HttpEntity httpEntity) {
                progressStage.close();
                SQLiteUtils.insertLog("登陆接口返回：" + httpEntity.toString());
                if (httpEntity.getCode() == 1) {
                    StageUIHelper.toCustomUI("库房管理", "house-view.fxml");
                } else {
                    StageUIHelper.getInstance().showTipAlert(httpEntity.getMsg());
                }
            }

            @Override
            public void onFail(Call call, String e) {
                progressStage.close();
                SQLiteUtils.insertLog("登陆接口错误：" + e);
                StageUIHelper.getInstance().showTipAlert(e);
            }
        });

    }

    @FXML
    protected void onSettingClick() {
        //弹出输入设置密码的对话框，校验密码是否正确
        showToSetting("请输入设置密码!");
    }

    private void showToSetting(String msg) {
        StageUIHelper.getInstance().showInputDialog(msg, new Consumer<String>() {
            @Override
            public void accept(String s) {
                SQLiteUtils.insertLog("输入设置密码：" + s);
                if (App.globalVal.settingPwd.equals(s)) {
                    StageUIHelper.getInstance().setTitle("设置")
                            .setFxml("setting-view.fxml")
                            .setPercent(0.3f)
                            .show();
                } else {
                    StageUIHelper.getInstance().showTipAlert("密码输入错误");
                }
            }
        });
    }


}